package com.sk.bilibili.view.activity;

import static com.sk.bilibili.utils.StringUtils.str;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;

import com.bumptech.glide.Glide;
import com.sk.bilibili.R;
import com.sk.bilibili.adapter.pager.live.LiveFragmentAdapter;
import com.sk.bilibili.bean.Focus;
import com.sk.bilibili.bean.Live;
import com.sk.bilibili.bean.Member;
import com.sk.bilibili.databinding.ActivityLiveBinding;
import com.sk.bilibili.event.LiveEvent;
import com.sk.bilibili.model.activity.LiveModel;
import com.sk.bilibili.utils.StringUtils;
import com.sk.bilibili.utils.ViewUtils;
import com.sk.bilibili.view.diy.bottomDialog.ShareBottomDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class LiveActivity extends BaseActivity<ActivityLiveBinding, LiveModel> {

    public static void start(Context context, Live live) {
        Intent intent = new Intent(context, LiveActivity.class);
        intent.putExtra("live", live);
        context.startActivity(intent);
    }

    private Live live;
    private Member member;
    private ConstraintLayout.LayoutParams landscapeParam;
    private ConstraintLayout.LayoutParams portraitParam;
    private ShareBottomDialog shareBottomDialog;

    @Override
    protected void afterSetView() {
        live = (Live) getIntent().getSerializableExtra("live");
        member = live.getExtra().getMember();
        model.addHistory(live);
        initData();
        initView();
        bindEvent();
    }

    private void initData() {
        landscapeParam = model.getLandscapeParam();
        portraitParam = model.getPortraitParam();
        model.checkLive(live);//确保URL存在 若没有需要再请求
        shareBottomDialog = new ShareBottomDialog(6,live.getId());
    }

    private void bindEvent() {
        bind.tvFocus.setOnClickListener(v -> {
            ViewUtils.switchFocus(bind.tvFocus, member);
            model.switchFocus(member.getId());
        });
        bind.avAvatar.setOnClickListener(v -> MemberActivity.start(this, live.getMemberId()));
    }

    private void initView() {
        initHeader();
        initViewPager();
    }

    private void initHeader() {
        bind.avAvatar.setRes(member.getAvatar(), member.getExtra().getPendant());
        bind.tvFansNum.setText(str(member.getFansNum()));
        ViewUtils.handleFocus(bind.tvFocus, member);
    }

    private void initViewPager() {
        bind.vpShow.setAdapter(new LiveFragmentAdapter(getSupportFragmentManager(), live));
        bind.tlShow.setupWithViewPager(bind.vpShow);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleLiveEvent(LiveEvent liveEvent) {
        switch (liveEvent.getType()) {
            case 0:
                bind.lpvShow.sendDanmu(liveEvent.getDanmu());
                break;
            case 1:
                finish();
                break;
            case 2:
                shareBottomDialog.show(getSupportFragmentManager());
                break;
            case 3:
                changeFullScreen(liveEvent.getFullScreen());
                break;
        }
    }

    private void changeFullScreen(Boolean fullScreen) {
        WindowManager.LayoutParams params = getWindow().getAttributes();
        if (fullScreen) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            bind.lpvShow.setLayoutParams(landscapeParam);
            params.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            bind.lpvShow.setLayoutParams(portraitParam);
            params.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
        bind.clShow.setVisibility(fullScreen ? View.GONE : View.VISIBLE);
        getWindow().setAttributes(params);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (bind.lpvShow.exitFullScreen()) return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.lpvShow.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        bind.lpvShow.pause();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        bind.lpvShow.resume();
        EventBus.getDefault().register(this);
    }

    public void checkLiveSuccess() {
        bind.lpvShow.play(live);
    }

    public void switchFocusSuccess(Focus focus) {
        toast(focus == null ? "取消关注成功" : "关注成功");
    }

}